package com.hf.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.hf.entity.Modules;
import com.hf.service.IModulesService;
import com.hf.util.Result;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author eight
 * @since 2021-06-06
 */
@RestController
@RequestMapping("/modules")
public class ModulesController {
	@Autowired
	IModulesService iModulesService;

	/**
	 * 模块查询，以树形结构展示，localhost:8080/modules/getModulesTree
	 * 
	 * @return
	 */
	@GetMapping("/getModulesTree")
	public Object getModulesTree() {
		List<Modules> list = iModulesService.getModulesTree();
		return list;
	}

	/**
	 * 获取所有模块--根据角色id勾选该角色所拥有的的模块
	 * 
	 * @param rid
	 * @return
	 */
	@GetMapping("/getModulesTreeByRoleId")
	public Object getModulesTreeByRoleId(Integer rid) {
		List<Modules> list = iModulesService.getModulesTreeByRoleId(rid);
		return list;
	}

	/**
	 * 添加 localhost:8080/modules/modules?moduleName=刘备
	 * 
	 * @param u
	 * @return
	 */
	@PostMapping("/insert")
	public Object insert(Modules m) {
		if (iModulesService.save(m)) {
			return new Result(0, "添加成功");
		}
		return new Result(1, "添加失败");

	}

	/**
	 * 删除 localhost:8080/modules/delete?id=1
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("/delete")
	public Object delete(Integer id) {
		if (iModulesService.removeById(id)) {
			return new Result(0, "删除成功");
		}
		return new Result(1, "删除失败");

	}

	/**
	 * 修改 localhost:8080/modules/update?moduleName=张飞&id=2
	 * 
	 * @param u
	 * @return
	 */
	@PostMapping("/update")
	public Object update(Modules m) {

		if (iModulesService.saveOrUpdate(m)) {
			return new Result(0, "修改成功");
		}
		return new Result(1, "修改失败");
	}

}
